package com.example.yimai.config;

import com.example.yimai.interceptor.JWTAuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JWTAuthorizationInterceptor jwtAuthorizationInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtAuthorizationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/user/login",
                        "/user/register",
                        "/product/query",
                        "/product/get/**",
                        "/product/getSameCategoryProducts/**",
                        "/news/query",
                        "/category/all",
                        "/category/top",
                        "/category/child/**",
                        "/captcha/image",
                        "/captcha/verify",
                        "/alipay/notify",
                        "/alipay/pay/**",
                        "/elasticsearch/"
                );
    }


}